package io.github.iogogogo.test;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by tao.zeng on 2024/12/2.
 */
public class SQLTimestampReplacer {

    @Test
    public void test() throws IOException {


        String sql = "time >= 1733108311656ms AND time < 1733108311690ms";

        // 定义正则表达式匹配时间戳整数
        String regex = "(\\d{13})(ms)";

        // 偏移量，例如加上1000
        long offset = 1001;

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(sql);
        StringBuffer sb = new StringBuffer();

        while (matcher.find()) {
            long timestamp = Long.parseLong(matcher.group(1));
            long newTimestamp = timestamp + offset; // 替换逻辑
            matcher.appendReplacement(sb, newTimestamp + "ms");
        }

        matcher.appendTail(sb);
        System.out.println("转换后的SQL: " + sb.toString());
    }
}
